﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using SuperMicroWave.Common;
using SuperMicroWave.LogFile;
using SuperMicroWave.Models;
using SuperMicroWave.Views;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Windows;
using static SuperMicroWave.Common.ConnectInfo;

namespace SuperMicroWave.ViewModels
{
    public partial class MainViewModel : ObservableRecipient
    {
        public MainViewModel() 
        {
            Thread thread = new Thread(new ThreadStart(UpdateState));
            thread.IsBackground = true;
            thread.Start();

            InitGlobalValue();

            //----------------------绘图界面----------------------
            CurveView cv = new CurveView();
            //----------------------方法界面----------------------
            MethodView mv = new MethodView();    
            //----------------------状态监测界面------------------
            StatusView sv = new StatusView();

            contentList.Add(cv);
            contentList.Add(mv);
            contentList.Add(sv);
            mainContent = contentList[0];
        }

        #region 数据源
        public List<UIElement> contentList = new List<UIElement>();

        [ObservableProperty]
        public UIElement mainContent;

        /// <summary>
        /// 通信连接状态
        /// </summary>
        [ObservableProperty]
        public bool connectState;

        [ObservableProperty]
        public string sysState = "";

        /// <summary>
        /// 磁控管温度
        /// </summary>
        [ObservableProperty]
        public double runTimeT1;

        /// <summary>
        /// 罐内实时温度
        /// </summary>
        [ObservableProperty]
        public double runTimeT;

        /// <summary>
        /// 实时压力
        /// </summary>
        [ObservableProperty]
        public double runTimeP1;

        /// <summary>
        /// 磁控管功率
        /// </summary>
        [ObservableProperty]
        public double runTimeP2;

        /// <summary>
        /// 系统时间
        /// </summary>
        [ObservableProperty]
        public string sysDateTime = "";
        #endregion


        #region 命令函数
        [RelayCommand]
        public void TabChanged(object obj)
        {
            if (obj == null) return;

            int i = Convert.ToInt32(obj);
            MainContent = contentList[i];
        }
        #endregion


        #region 功能函数
        public void InitGlobalValue()
        {
            GlobalValue.sysParams.MethodName = INIFile.InifileReadValue("SYSPARAMS", "MethodName", INIFile.iniPath);
            GlobalValue.sysParams.MethodFilePath = INIFile.InifileReadValue("SYSPARAMS", "MethodFilePath", INIFile.iniPath);

            GlobalValue.sysParams.PreScale = double.Parse(INIFile.InifileReadValue("Test", "PreScale", INIFile.iniPath));
            GlobalValue.sysParams.StabScale = double.Parse(INIFile.InifileReadValue("Test", "StabScale", INIFile.iniPath));
            GlobalValue.sysParams.StabTime = int.Parse(INIFile.InifileReadValue("Test", "StabTime", INIFile.iniPath));
            GlobalValue.sysParams.LeakV = double.Parse(INIFile.InifileReadValue("Test", "LeakV", INIFile.iniPath));

            GlobalValue.sysParams.AutoCleanPreMax = double.Parse(INIFile.InifileReadValue("Test", "AutoCleanPreMax", INIFile.iniPath));
            GlobalValue.sysParams.AutoCleanPreMin = double.Parse(INIFile.InifileReadValue("Test", "AutoCleanPreMin", INIFile.iniPath));

            if (!File.Exists(GlobalValue.sysParams.MethodFilePath))
            {
                MessageBox.Show("方法文件不存在，已选择默认方法");
                GlobalValue.sysParams.MethodFilePath = "Xml/MethodData.xml";
                GlobalValue.sysParams.MethodName = "MethodData";
            }

            MethodModel.ReadMethodXML(GlobalValue.sysParams.MethodFilePath, ref GlobalValue.methodData);

            GlobalValue.HeatTable.LoadHashTable();
            GlobalValue.CoolTable.LoadHashTable();
        }

        /// <summary>
        /// 更新仪器状态信息
        /// </summary> 
        public void UpdateState()
        {
            while (true)
            {
                ConnectState = GlobalValue.modBusTcp.IsConnected();
                SysDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");  //系统时间               

                switch (GlobalValue.NowState.CurState)
                {
                    case InsState.DEV_IDLE:
                        SysState = "空闲";
                        break;
                    case InsState.DEV_HEAT:
                        SysState = "升温";
                        break;
                    case InsState.DEV_PAUSE:
                        SysState = "暂停";
                        break;
                    case InsState.DEV_ERROR:
                        SysState = "掉线";
                        break;
                }

                if(ConnectState)
                {
                    RunTimeT = GlobalValue.NowState.T;
                    RunTimeT1 = GlobalValue.NowState.T1;
                    RunTimeP1 = GlobalValue.NowState.P1;
                    RunTimeP2 = GlobalValue.NowState.P2;
                }
                else
                {
                    RunTimeT = 0;
                    RunTimeT1 = 0;
                    RunTimeP1 = 0;
                    RunTimeP2 = 0;
                }

                Thread.Sleep(1000);
            }
        }
        #endregion
    }
}
